package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataFont extends NullField implements ITag {
	public static String DEFAULT_ID = "_Font";
		DataString face = new DataString();
		DataString style = new DataString();
		DataSize pointSize = new DataSize(10);
		DataSize pointSizeView = new DataSize(10);
public DataFont() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FACE))
		{
			face = new DataString();
			face.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.STYLE))
		{
			style = new DataString();
			style.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.POINT_SIZE))
		{
			pointSize = new DataSize(TagModifier.SIZE_TYPE_SIZE,10);
			pointSize.buildFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else if( !NameHelper.isSystemName(element.getName()) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.FONT,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FACE))
		{
			face.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.STYLE))
		{
			style.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.POINT_SIZE))
		{
			pointSize.copyFromgrfxML(element,TagModifier.SIZE_TYPE_SIZE);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.FONT,""+element.getStartLineNo()} );
		}
	}
}
public DataFont getClone() {

	DataFont newFont = new DataFont();

	newFont.face = face.getClone();
	newFont.pointSize = pointSize.getClone(TagModifier.SIZE_TYPE_SIZE);
	newFont.style = style.getClone();

	return newFont;
}
public DataFont getClone(String path) {

	DataFont newFont = new DataFont();

	GrfxMLStoreProcessor.setCloneName( path,this,newFont );

	newFont.face = face.getClone(path);
	newFont.pointSize = pointSize.getClone(TagModifier.SIZE_TYPE_SIZE,path);
	newFont.style = style.getClone(path);

	return newFont;
}
public DataString getFace() {
	return face;
}
public DataSize getPointSize() {
	return pointSize;
}
/**
 *
 * @return org.one.stone.soup.grfxML.DataString
 */
public DataString getStyle() {
	return style;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(face,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(style,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(pointSize,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	if(face==oldObj)
		face=(DataString)newObj;

	if(style==oldObj)
		style=(DataString)newObj;

	if(pointSize==oldObj)
		pointSize=(DataSize)newObj;
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	if(face==oldObj)
		face=(DataString)newObj;

	if(style==oldObj)
		style=(DataString)newObj;

	if(pointSize==oldObj)
		pointSize=(DataSize)newObj;
}
public void resetChanged() {
	super.resetChanged();
	face.resetChanged();
	style.resetChanged();
	pointSize.resetChanged();
	pointSizeView.resetChanged();
}
protected void setFace(DataString newFace) {
	face = newFace;
}
protected void setPointSize(DataSize newSize) {
	pointSize = newSize;
}
protected void setStyle(DataString newStyle) {
	style = newStyle;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += face.togrfxML(Token.FACE);
	xml += style.togrfxML(Token.STYLE);
	xml += pointSize.togrfxML(new DataSize(10),Token.POINT_SIZE);
	xml += "</" + name + ">\n";
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t) {
	try {
		GrfxMLProcessor.translate(pointSize, TagModifier.SIZE_TYPE_SIZE, t, pointSizeView);
	} catch (LockException le) {
		le.printStackTrace();
	}
	if (face.hasChanged() || style.hasChanged() || pointSize.hasChanged() || pointSizeView.hasChanged()) {
		setChanged();
	}
}
}
